// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) // 使用当前云环境

// 云函数入口函数
exports.main = async (event, context) => {
  const db = cloud.database()
  const wxContext = cloud.getWXContext()
  const openid = wxContext.OPENID

  // 要先获取到学生所加入的班级
  const classes = await db.collection('class').get()

  const class_id = classes.data.filter(i => {
    return i.members.find(m => m.openid === openid)
  })

  // 获取到学生可以看到的测试
  const test = await db.collection('test').get()

  const result = test.data.filter(t => {
    return class_id.find(c => t.class_id === c._id)
  })

  return result
}